package sisi.suppliers;

import java.util.List;

import javax.persistence.EntityManager;
/**
 * TraderController.java - Controller class for the <i>traders database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 18/12/2013
 * 
 */
public class TraderController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Trader[] get(String cerca) {
		List<Trader> lista = getList(cerca);
			return lista.toArray(new Trader[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Trader> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.tradername) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Trader as c " + cWhere 
					+ " order by c.tradername ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Trader> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Trader> getListxIDSupplier(Integer idSupplier) {

		String cWhere = " WHERE c.traderid = :cerca ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Trader as c " + cWhere 
					+ " order by c.seq ");

			q.setParameter("cerca", idSupplier );

			List<Trader> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	
	public Trader getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Trader traderx = em.find(Trader.class, id);
			return traderx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Trader add(Trader trader) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(trader);
			em.getTransaction().commit();
			trader = refresh(trader);
		} finally {
			em.close();
			return trader;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Trader trader) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Trader traderx = em.find(Trader.class, trader.getIdunico());
			traderx.setId(trader.getId());
			traderx.setSeq(trader.getSeq());
			traderx.setIdunico(trader.getIdunico());
			traderx.setPhysicalid(trader.getPhysicalid());
			traderx.setPhysicalname(trader.getPhysicalname());
			traderx.setPhysicaltype(trader.getPhysicaltype());
			traderx.setTraderid(trader.getTraderid());
			traderx.setTradername(trader.getTradername());
			traderx.setTradertype(trader.getTradertype());
			traderx.setDt(trader.getDt());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Trader refresh(Trader trader) {

		Trader traderx = trader;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			traderx = em.find(Trader.class, trader.getIdunico());
			em.flush();
			em.refresh(traderx);
		} finally {
			em.close();
			return traderx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Trader trader) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Trader traderx = em.find(Trader.class, trader.getIdunico());
			em.remove(traderx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//

